<?php
namespace app\common\model;

use think\Db;
use think\Model;

/**   YSF
 *    订单模型  Model
 *    Date: 2018/7/30
 * Class OrderModel
 * @package app\common\model
 */
class OrderModel extends Model
{
    // 表名
    protected $name = 'orders';

    // 列表查询
    public function getAll($where, $join, $page, $pageSize, $field, $order, $dbName)
    {
        $result = Db::name($dbName)->where($where)
                    ->join($join)
                    ->field($field)
                    ->page($page, $pageSize)
                    ->order($order)
                    ->select();
//        echo Db::name($dbName)->getLastSql();die();
        return $result;
    }

    // 连表数量查询
    public function getCount($where, $join, $dbName)
    {
        $result = Db::name($dbName)
                    ->where($where)
                    ->join($join)
                    ->count();
        return $result;
    }

    // 单个查询
    public function getOne($where=[], $join=[], $field='*', $dbName='orders')
    {
        $result = Db::name($dbName)
                    ->where($where)
                    ->join($join)
                    ->field($field)
                    ->find();
        return $result;
    }

    // 统计积分
    public function getMoneySum($where, $field)
    {
        $result = $this->where($where)
                ->sum($field);
        if (empty($result)) {
            return 0.00;
        }
        return $result;
    }

    // 不连表数量查询
    public function count($where)
    {
        $result = $this->where($where)->count();
        return $result;
    }

    // 分组查询
    public function getGroup($where, $join, $group, $field, $order)
    {
        $data = $this->alias('a')
                ->where($where)
                ->field($field)
                ->order($order)
                ->join($join)
                ->group($group)
                ->select();
        return $data;
    }

    // 连表加别名查询列表
    public function getJoinAll($where=[], $join=[], $field='*', $order='')
    {
        $result = $this->alias('a')
                    ->where($where)
                    ->join($join)
                    ->field($field)
                    ->order($order)
                    ->select();
        return $result;
    }
     /**
     * @desc   添加
     * @param  无
     * @date   2018-08-15
     * @author lcx
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public function add($data)
    {
        return $this->insertGetId($data);
    }

    // 修改订单状态
    public function edit($where, $data)
    {
        $this->where($where)->update($data);
    }

}